package com.lian.hope.interceptors;

import com.lian.hope.utils.JwtUtil;
import io.swagger.models.HttpMethod;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class LoginInterceptor implements HandlerInterceptor, Ordered {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        // 1.获取请求的 url
        String url = request.getRequestURL().toString();
        log.info("请求的 URL:{}", url);

        // 2.判断请求的 url 中是否包含 login，如果包含，说明是登录操作，放行
        if (url.contains("login") ) {
            log.info("登录操作，放行");
            return true;
        }
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            System.out.println("OPTIONS 请求，放行");
            return true;
        }

        // 3.获取请求头中的 Authorization
        String token = request.getHeader("Authorization");
        log.info("请求头:{}", request.getHeader("Authorization"));
        // 4.判断令牌是否存在，如果不存在，返回错误结果(未登录)
        if (!StringUtils.hasLength(token)) {
            response.setStatus(401);
            return false;
        }

        // 5.解析 token，如果解析失败，返回错误结果
        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            log.info("claims:{}", claims);
            request.setAttribute("user", claims);
            return true;
        } catch (Exception e) {
            log.error("解析令牌失败", e);
            response.setStatus(401);
            return false;
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}